/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AjoutTypeRobot.java
 *
 * Created on 24 nov. 2011, 11:17:25
 */
package robotlion.view;

import Tools.JDOMManager;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author test
 */
public class AjoutTypeRobot extends javax.swing.JFrame {

    private JDOMManager myJDOM;
    private javax.swing.table.DefaultTableModel tableModel;
    
    private void setjTable1() 
    {
        tableModel = new javax.swing.table.DefaultTableModel();
        tableModel.addColumn("Nom");
        tableModel.addColumn("Vitesse");
        tableModel.addColumn("Niveau accessibilite");
        
        ArrayList<String> listRobotTypes = myJDOM.getListRobot();
        String robot="";
        for (int i = 0; i < listRobotTypes.size(); i++) {
            robot = listRobotTypes.get(i);
            tableModel.addRow(new Object[] {robot.substring(0,robot.indexOf('|')),
           robot.substring(robot.indexOf('|')+1,robot.lastIndexOf('|')),
          robot.substring(robot.lastIndexOf('|')+1,robot.length())});   
        }
        
        jTable1.setModel(tableModel);
    }

    /** Creates new form AjoutTypeRobot */
    public AjoutTypeRobot(JDOMManager myJDOM) {
        initComponents();
        this.myJDOM=myJDOM;
        setjTable1();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(robotlion.view.RobotLion.class).getContext().getResourceMap(AjoutTypeRobot.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nom", "Vitesse", "Niveau Access", "Algorithme"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
    myJDOM.save();
    this.setVisible(false);
}//GEN-LAST:event_jButton1MouseClicked

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
    String nomRobot = (String)jTable1.getValueAt(jTable1.getSelectedRow(), 0);
    if(myJDOM.deleteRobot(nomRobot))
        tableModel.removeRow(jTable1.getSelectedRow());
}//GEN-LAST:event_jButton3MouseClicked

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
    String nom      = "";
    String vitesse  = "";
    String Access   = "";
    
    nom         = JOptionPane.showInputDialog("Donner un nom.");
    vitesse     = JOptionPane.showInputDialog("Donner une vitesse.");
    Access      = JOptionPane.showInputDialog("Donner un Niveau d'accessibilité.");
    
    tableModel.addRow(new Object[] {nom, vitesse, Access});
    
    myJDOM.addRobot(nom, vitesse, Access);
}//GEN-LAST:event_jButton2MouseClicked

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
